// 数据库初始化种子数据
db = db.getSiblingDB(process.env.MONGO_DATABASE || 'evaluation_system');

// 清理现有数据
print('Cleaning existing data...');
db.users.drop();
db.questions.drop();
db.evaluations.drop();
db.auditLogs.drop();

// 创建管理员用户
print('Creating admin user...');
db.users.insertOne({
  email: 'admin@example.com',
  password: '$2a$10$YourHashedPasswordHere', // 请在生产环境中更改
  name: '系统管理员',
  role: 'admin',
  isActive: true,
  createdAt: new Date(),
  updatedAt: new Date()
});

// 创建测试教师用户
print('Creating test teachers...');
const teachersList = [
  {
    email: 'teacher1@example.com',
    password: '$2a$10$YourHashedPasswordHere',
    name: '测试教师1',
    role: 'teacher',
    isActive: true,
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    email: 'teacher2@example.com',
    password: '$2a$10$YourHashedPasswordHere',
    name: '测试教师2',
    role: 'teacher',
    isActive: true,
    createdAt: new Date(),
    updatedAt: new Date()
  }
];
db.users.insertMany(teachersList);

// 创建测试学生用户
print('Creating test students...');
const studentsList = [];
for (let i = 1; i <= 5; i++) {
  studentsList.push({
    email: `student${i}@example.com`,
    password: '$2a$10$YourHashedPasswordHere',
    name: `测试学生${i}`,
    role: 'student',
    isActive: true,
    createdAt: new Date(),
    updatedAt: new Date()
  });
}
db.users.insertMany(studentsList);

// 创建测试题目
print('Creating test questions...');
const questionsList = [
  {
    title: '测试题目1',
    content: '这是测试题目1的内容',
    type: 'single_choice',
    difficulty: 'easy',
    options: ['选项A', '选项B', '选项C', '选项D'],
    correctAnswer: 'A',
    score: 10,
    createdBy: db.users.findOne({ role: 'teacher' })._id,
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    title: '测试题目2',
    content: '这是测试题目2的内容',
    type: 'multiple_choice',
    difficulty: 'medium',
    options: ['选项A', '选项B', '选项C', '选项D'],
    correctAnswer: ['A', 'B'],
    score: 15,
    createdBy: db.users.findOne({ role: 'teacher' })._id,
    createdAt: new Date(),
    updatedAt: new Date()
  }
];
db.questions.insertMany(questionsList);

// 创建测试评估记录
print('Creating test evaluations...');
const allStudents = db.users.find({ role: 'student' }).toArray();
const allQuestions = db.questions.find().toArray();

allStudents.forEach(student => {
  allQuestions.forEach(question => {
    db.evaluations.insertOne({
      userId: student._id,
      questionId: question._id,
      answer: question.type === 'single_choice' ? 'A' : ['A'],
      score: Math.floor(Math.random() * question.score),
      feedback: '这是测试评估反馈',
      evaluatedBy: db.users.findOne({ role: 'teacher' })._id,
      createdAt: new Date(),
      updatedAt: new Date()
    });
  });
});

// 创建审计日志
print('Creating audit logs...');
[
  {
    userId: db.users.findOne({ role: 'admin' })._id,
    action: 'USER_CREATE',
    details: '创建新用户',
    timestamp: new Date()
  },
  {
    userId: db.users.findOne({ role: 'teacher' })._id,
    action: 'QUESTION_CREATE',
    details: '创建新题目',
    timestamp: new Date()
  }
].forEach(log => db.auditLogs.insertOne(log));

print('Database seeding completed successfully!');
